package io.github.jast90.internship.service;

import io.github.jast90.internship.entity.User;
import io.github.jast90.internship.form.UserChangePasswordForm;
import io.github.jast90.internship.mapper.UserMapper;
import io.github.jast90.internship.util.SessionUtil;
import io.github.jast90.mybatis.pagination.domain.Page;
import io.github.jast90.mybatis.pagination.domain.PageRequest;
import io.github.jast90.web.common.result.CommonResult;
import io.github.jast90.web.common.result.MyResult;
import io.github.jast90.web.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by jast90 on 2021/3/8
 */
@Service
public class UserService extends BaseService<UserMapper, User> {
    @Autowired
    private PasswordEncoder passwordEncoder;

    private static final String defaultPassword = "123456";

    @Autowired
    private SessionUtil sessionUtil;

    public CommonResult<User> login(String loginName, String password) {
        User user = mapper.selectByUsernameAndPassword(loginName);
        if(user==null){
            return new CommonResult<>(MyResult.FAIL);
        }
        if(passwordEncoder.matches(password,user.getPassword())){
            return new CommonResult<>(MyResult.SUCCESS,user);
        }else {
            return new CommonResult<>(MyResult.FAIL);
        }
    }

    @Override
    public CommonResult<User> add(User entity) {
        entity.setPassword(passwordEncoder.encode(entity.getPassword()!=null?entity.getPassword():defaultPassword));
        return super.add(entity);
    }

    @Override
    public CommonResult<User> updateById(User entity) {
        return super.updateById(entity);
    }

    public CommonResult updatePassword(UserChangePasswordForm form){
        User user = mapper.selectByPrimaryKey(form.getId());
        if(user == null || !passwordEncoder.matches(form.getOldPassword(),user.getPassword())){
            return new CommonResult<>(MyResult.FAIL);
        }
        if(form.getNewPassword() != null){
            form.setPassword(passwordEncoder.encode(form.getNewPassword()));
            if(mapper.updatePassword(form)>0){
                return new CommonResult<>(MyResult.SUCCESS);
            }
        }
        return new CommonResult<>(MyResult.FAIL);
    }

    @Override
    public CommonResult<List<User>> queryListByCondition(User user) {
        if(sessionUtil.teacher()){
            user.setTeacherId(sessionUtil.getUid());
        }
        if(sessionUtil.student()){
            user.setStudentId(sessionUtil.getUid());
        }
        return super.queryListByCondition(user);
    }

    @Override
    public CommonResult<Page<User>> queryPage(PageRequest pageRequest, User entity) {
        if(sessionUtil.teacher()){
            entity.setTeacherId(sessionUtil.getUid());
            entity.setUserType((byte)3);
        }
        return super.queryPage(pageRequest, entity);
    }
}
